package com.truiton.screencapture;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

import dalvik.system.BaseDexClassLoader;
import dalvik.system.DexClassLoader;
import dalvik.system.PathClassLoader;

public class WelcomeActivity extends AppCompatActivity {
    public final static String[] MUSTPERMISSION = {Manifest
            .permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        if (CheckPermission(this, MUSTPERMISSION) != null) {
            showPermissioinDialog();
            return;
        }
        init();
    }

    private void init() {
        startActivity(new Intent(this, CaptureActivity.class));
        finish();
    }

    private void showPermissioinDialog() { //显示权限申请对话框
        String appname = getString(R.string.app_name);
        if (CheckShowPermissionRationale(this, MUSTPERMISSION)) {
            DialogUtils.showCommonDialog(this, 0, "打开" + appname, "尊敬的" + appname + "用户," +
                            "为了使" + appname + "能保存已录制的影像,需要获取以下权限:\n1,访问外置存储",
                    "下一步", "取消", new DialogInterface
                            .OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(WelcomeActivity.this,
                                    loadRequirePermissions(WelcomeActivity.this, MUSTPERMISSION), 0);
                        }
                    }, new DialogInterface
                            .OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    }, false);
        } else {
            //直接跳转到app设置界面
            String setting = "\n设置路径:设置-->应用-->" + appname + "-->权限-->存储空间";
            DialogUtils.showCommonDialog(this, 0, "打开" + appname, "尊敬的" + appname + "用户," +
                    "为了使" + appname + "能保存已录制的影像,需要获取以下权限:\n" +
                    "1,访问外置存储的权限" +
                    setting, "去打开", "退出" + appname, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, 1);
                }
            }, new DialogInterface
                    .OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            }, false);
        }
    }

    public static String[] loadRequirePermissions(Context context, String... permissions) {
        List<String> unGetPermissions = new ArrayList<>();
        for (int index = 0; index < permissions.length; index++) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(context, permissions[index])) {
                unGetPermissions.add(permissions[index]);
            }
        }
        //unGetPermissionArray的大小必须大于0
        String[] unGetPermissionArray = new String[unGetPermissions.size()];
        for (int index = 0; index < unGetPermissionArray.length; index++) {
            unGetPermissionArray[index] = unGetPermissions.get(index);
        }
        return unGetPermissionArray;
    }

    private boolean CheckShowPermissionRationale(Context context, String... permissions) {
        //找到一个没有权限的
        String permission = CheckPermission(context, permissions);
        if (permission != null) {
            return ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
        }
        return false;
    }

    private String CheckPermission(Context context, String... permissions) {
        for (int index = 0; index < permissions.length; index++) {
            //找到一个没有权限的
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(context, permissions[index])) {
                return permissions[index];
            }
        }
        return null;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0) {
            if (CheckPermission(this, permissions) == null) {
                init();
            } else {
                showPermissioinDialog();
            }
        }
    }
}
